﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using AutoTestEntry.Data;
using AutoTestEntry.Utility;

namespace AutoTestEntry.SpecificUtility
{
    class EnvInitializer
    {
        private static FtpDownloader ftpDownloader = new FtpDownloader();


        public static void ReadGlobalInfo()
		{
			GlobalInfo.ReadLocalInfo();
		}

		/// <summary>
		/// 如果有新版本，则返回新的版本号，否则返回 ""
		/// </summary>
		/// <returns></returns>
		public static String CheckForNewVersion()
		{
			String ftpVersion = GetVersionFromFTP();
			if (ftpVersion == null)
			{
				GlobalInfo.NewVersion = null;
			}
			else if (ftpVersion.Equals(GlobalInfo.Version))
			{
				GlobalInfo.NewVersion = "";
			}
			else
			{
				GlobalInfo.NewVersion = ftpVersion;
			}
			return GlobalInfo.NewVersion;
		}

        public static String IsNewVersionAvailable()
        {
			return GlobalInfo.NewVersion;
        }

        private static String GetVersionFromFTP()
        {
            String[] version = ftpDownloader.GetBriefFileList(GlobalInfo.FtpAddress + "/" + GlobalInfo.FtpVersionPath);
			if (version == null || version.Length == 0)
			{
				return null;
			}
			else
			{
				return version[0];
			}
        }
    }
}
